package cn.admin.remoting.util;

import java.lang.reflect.Field;

public class ReflectionUtil {

    public static Object getPrivateField(Object instance,String name) {

        Object result = null;
        Class type = instance.getClass();
        do {
            try {
                Field f = type.getDeclaredField(name);
                f.setAccessible(true);
                result = f.get(instance);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        } while (result == null && (type = type.getSuperclass()) != null);

        return result;

    }

    public static void setStaticField(Class<?> type,String name,Object value)
            throws NoSuchFieldException, IllegalAccessException {
        Field f = type.getDeclaredField(name);
        f.setAccessible(true);
        f.set(null,value);
    }

}
